package com.guigu.qingcheng.controller;

import com.guigu.qingcheng.pojo.TbSku;
import com.guigu.qingcheng.pojo.dto.GoodsDto;
import com.guigu.qingcheng.pojo.vo.Goods;
import com.guigu.qingcheng.pojo.vo.SkuGoodsVo;
import com.guigu.qingcheng.service.GoodsService;
import com.guigu.qingcheng.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/goods")
@Api(tags = "GoodsController")
public class GoodsController {

    @Autowired
    private GoodsService goodsService;

    @ApiOperation(value = "添加商品")
    @PostMapping("addGoods")
    public R addGoods(@RequestBody Goods goods){
        goodsService.saveGoods(goods);
        return R.ok();
    }

    /**
     * 保存
     * @param goods
     * @return
     */
    @PostMapping("/save")
    public R save(@RequestBody Goods goods){
        goodsService.addGoods(goods);
        return R.ok().message("添加成功");
    }

    @ApiOperation(value = "根据商品id查找商品")
    @GetMapping("queryByGoodsName/{id}")
    public R queryByGoodsName(@PathVariable("id") String id){
        GoodsDto goodsDto= goodsService.queryByGoodsName(id);
        return R.ok().data("goodsDto",goodsDto);
    }

    @ApiOperation(value = "根据商品名称查找商品  不分页")
    @GetMapping("queryByGoodsLikeName/{name}")
    public R queryByGoodsLikeName(@PathVariable("name") String name){
        List<TbSku> skus= goodsService.queryByGoodsLikeName(name);
        return R.ok().data("skus",skus);
    }

    @ApiOperation(value = "根据商品id查询商品")
    @GetMapping("/queryBySpuId/{spuId}")
    public Goods queryBySpuId(@PathVariable("spuId") String spuId){
        Goods goods = goodsService.queryBySpuId(spuId);
        return goods;
    }

    @PostMapping("/querySkuGoodsBySpec")
    public String querySkuGoodsBySpec(@RequestBody SkuGoodsVo skuGoodsVo){
        String s = goodsService.querySkuGoodsBySpec(skuGoodsVo);
        return s;
    }

}
